﻿using System;
using System.Collections.Generic;
using Toast.Compression;
using Toast.Core.Interfaces;

namespace Toast.Core
{
    public class DefaultCompressionDictionary : IHideObjectMembers
    {
        readonly Dictionary<Type, ICompressionStrategy> _dictionary = new Dictionary<Type, ICompressionStrategy>();

        internal Dictionary<Type, ICompressionStrategy> Dictionary
        {
            get { return _dictionary; }
        }

        public ICompressionStrategy For<TContent>() where TContent : IContent
        {
            return For(typeof(TContent));
        }

        public ICompressionStrategy For(IContent content)
        {
            return For(content.GetType());
        }

        public ICompressionStrategy For(Type contentType)
        {
            return _dictionary.ContainsKey(contentType) ? _dictionary[contentType] : new NoCompression();
        }

        public void SetDefaultFor<TContent, TCompress>()
            where TContent : IContent
            where TCompress : ICompressionStrategy, new()
        {
            if (!_dictionary.ContainsKey(typeof(TContent)))
                _dictionary.Add(typeof(TContent), CompressorFactory.Get<TCompress>());
            else
            {
                _dictionary[typeof(TContent)] = CompressorFactory.Get<TCompress>();
            }
        }

        public void SetDefaultFor<TContent, TCompress>(CompressionLevel compressionLevel)
            where TContent : IContent
            where TCompress : ICompressionStrategy, new()
        {
            if (!_dictionary.ContainsKey(typeof(TContent)))
                _dictionary.Add(typeof(TContent), CompressorFactory.Get<TCompress>(compressionLevel));
            else
            {
                _dictionary[typeof(TContent)] = CompressorFactory.Get<TCompress>(compressionLevel);
            }
        }
    }
}